package com.atguigu.gulimail.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimail.seckill.service.SeckillService;
import com.atguigu.gulimail.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author fangy
 * @date 2022-10-24 23:48
 */
@Slf4j
@Controller
public class SeckillController {


    @Autowired
    SeckillService seckillService;


    /**
     *
     * 获取当前秒杀场次
     * @return
     */
    @ResponseBody
    @RequestMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){

        log.info("currentSeckillSkus正在执行。。。");
        List<SeckillSkuRedisTo> skus = seckillService.getCurrentSecKillSkus();

        return R.ok().setData(skus);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){

        String orderSn =  seckillService.kill(killId,key,num);

        model.addAttribute("orderSn",orderSn);
        //1、判断是否登录
        return "success";
    }

}
